<?php
/**
 *安全设置
 * @author yanghj 2014.5.12
 */
class Account_securityApp extends MemberbaseApp
{
    var $_user_id;
    var $_user_safety_mod;
    var $_email_log_mod;
    var $_mobile_log_mod;
    var $_user_mod;
    var $_member_from;

    function __construct()
    {
        $this->Account_security();
    }
    function Account_security()
    {
        parent::__construct();
        $this->_user_id = intval($this->visitor->get('user_id'));
        $this->_user_safety_mod = &m('usersafety');
        $this->_email_log_mod = &m('emaillog');
        $this->_mobile_log_mod = &m('mobilelog');
        $this->_user_mod = &m('member');
        $this->_member_from = isset($_GET['from']) ? trim($_GET['from']) : 'buyer';
        $this->assign('member_from', $this->_member_from);

    }

    function index()
    {
        //unset($_SESSION['mobile_ready_send_sms']);
        //var_dump($_SESSION['mobile_ready_send_sms']);
        //var_dump($_SESSION['mobile_code']);
        //var_dump($_COOKIE['mobile']);
        //var_dump($_SESSION['set_pay_password_step']);

        $sql = "SELECT u.password_level ,s.* FROM " . $this->_user_mod->table .
            " AS u  LEFT JOIN " . $this->_user_safety_mod->table .
            " AS s ON u.user_id=s.user_id WHERE u.user_id=" . $this->_user_id;
        $safety_list = $this->_user_safety_mod->getRow($sql);
        $certification_mod = &m('certification');
        $certification_data = $certification_mod->get(array('fields' => 'certification_type,status', 'conditions'    => 'user_id = ' . $this->_user_id, 'order' => 'certification_id desc'));
        if ($this->_member_from == 'seller') {
            $this->_curlocal(LANG::get('seller_center'), 'index.php?app=seller', 
                             LANG::get('my_account'),'index.php?app=account_security&from=seller',
                             LANG::get('my_safety'));
            $this->_config_seo('title', Lang::get('seller_center') . ' - ' . Lang::get('my_safety'));
        } else {
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', 
                             LANG::get('my_account'),'index.php?app=account_security&from=buyer', 
                             LANG::get('my_safety'));
            $this->_config_seo('title', Lang::get('buyer_center') . ' - ' . Lang::get('my_safety'));
        }
        if (!empty($safety_list['binding_email'])) {
            $safety_list['binding_email'] = hide_account($safety_list['binding_email'], 1);
        }
        if (!empty($safety_list['binding_mobile'])) {
            $safety_list['binding_mobile'] = hide_account($safety_list['binding_mobile'], 2);
        }

        /* 当前用户中心菜单 */
        $this->_curitem('my_safety');

        /* 当前所处子菜单 */
        $this->_curmenu('safety_list');
        $this->assign('safety_level', array(
            '1' => '低',
            '2' => '中',
            '3' => '高'));
        $this->assign('safety_list', $safety_list);
        $this->assign('certification', $certification_data);
        $this->assign('account_security_level', $this->account_security_level());       
        $this->display('account_security.index.html');
        

    }
    /**
     *绑定手机绑定邮箱与设置支付密码
     */
    function authen_verify()
    {
        $mode = isset($_GET['mode']) ? $_GET['mode'] : '';
        $step = isset($_GET['step']) ? intval($_GET['step']) : 1;
        $cur_name = '绑定邮箱';
        $template = 'binding_email.index.html';
        $accout_data = $this->_user_safety_mod->get(array('conditions'    => 'user_id = ' . $this->_user_id));
        switch ($mode) {
            case 'set_mobile': //设置绑定手机
                $cur_name = '绑定手机';
                $template = 'binding_mobile.index.html';                
                if($step==1)
                {
                    if($accout_data['binding_mobile'])
                    {
                        $this->show_warning("你已经绑定手机了,不能再绑定",'goback', 'index.php?app=account_security&from='.$this->_member_from);
                        return ;                      
                    }
                    
                }                
                if($step==2)
                {
                    if(!$accout_data['binding_mobile'])
                    {
                        $this->show_warning("request_error",'back_index', 'index.php');
                        return ;                      
                    }
                    
                }
                break;
            case 'edit_mobile': //修改绑定手机
                $cur_name = '修改绑定手机';
                $template = 'binding_mobile_edit.index.html';
                if($step==1)
                {
                    if(!$accout_data['binding_mobile'])
                    {
                        $this->show_warning("你还没有绑定手机",'立刻绑定', 'index.php?app=account_security&act=authen_verify&mode=set_mobile');
                        return ;                      
                    }
                    
                }           
                break;
            case 'set_email': //设置绑定邮箱
                $cur_name = '绑定邮箱';
                $template = 'binding_email.index.html';
                
                if($step==1)
                {
                    if($accout_data['binding_email'])
                    {
                        $this->show_warning("你已经绑定邮箱了,不能再绑定",'goback', 'index.php?app=account_security&from='.$this->_member_from);
                        return ;                      
                    }
                    
                }

                if($step==2)
                {
                    if(!$accout_data['binding_email'])
                    {
                        $this->show_warning("请求错误",'back_index', 'index.php');
                        return ;                      
                    }
                    
                }
                break;
            case 'edit_email': //修改绑定邮箱
                $cur_name = '修改绑定邮箱';
                $template = 'binding_email_edit.index.html';
                if($step==1)
                {
                    if(!$accout_data['binding_email'])
                    {
                        $this->show_warning("你还没有绑定邮箱",'立刻绑定', 'index.php?app=account_security&act=authen_verify&mode=set_email');
                        return ;                      
                    }
                    
                }            
                break;
            case 'set_pay_password': //设置支付密码
                $cur_name = '设置支付密码';
                $template = 'pay_password.index.html';                 
                $set_type = trim($_GET['set_type']);
                if($set_type == 'mobile')//手机方式设置
                {
                    $step = isset($_SESSION['set_pay_password_step'])? $step : 1;
                    if($step == 2 && $_SESSION['set_pay_password_step']==2)
                    {
                        $step = 2;                       
                    }                   
                    if($accout_data['pay_password'] && $step != 3)
                    {
                        $this->show_warning("你已经设置了支付密码",'goback', 'index.php?app=account_security&from='.$this->_member_from);
                         return ;                        
                    }
                    if($step == 3 && isset($_SESSION['set_pay_password_step']))
                    {
                        unset($_SESSION['set_pay_password_step']);                        
                    }
                    if(!$accout_data['binding_mobile'])
                    {
                       $this->show_warning("请先绑定手机再设置",'goback', 'index.php?app=account_security&act=authen_verify&mode=set_mobile&from='.$this->_member_from);
                        return ; 
                    }
                    $cur_name = '绑定手机方式设置支付密码';
                    $template = 'pay_password_mobile.index.html'; 
                    $accout_data['hide_mobile']= hide_account($accout_data['binding_mobile'], 2);
                    
                }
                
                if($set_type == 'email')//邮箱方式设置
                {
                    $step = isset($_SESSION['set_pay_password_step'])? $step : 1;
                    if($step == 2 && $_SESSION['set_pay_password_step']==2)
                    {
                        $step = 2;                       
                    }                   
                    
                    if($accout_data['pay_password'] && $step != 3)
                    {
                        $this->show_warning("你已经设置了支付密码",'goback', 'index.php?app=account_security&from='.$this->_member_from);
                         return ;                        
                    }
                    
                    if($step == 3 && isset($_SESSION['set_pay_password_step']))
                    {
                        unset($_SESSION['set_pay_password_step']);                        
                    }
                    
                    if(!$accout_data['binding_email'])
                    {
                       $this->show_warning("请先绑定邮箱再设置",'goback', 'index.php?app=account_security&act=authen_verify&mode=set_email&from='.$this->_member_from);
                        return ; 
                    }
                    $cur_name = '绑定邮箱方式设置支付密码';
                    $template = 'pay_password_email.index.html'; 
                    $accout_data['hide_email']= hide_account($accout_data['binding_email'], 1);
                    
                }
                break;
            case 'edit_pay_password': //修改支付密码
                if(!$accout_data['pay_password'])
                {
                    $this->show_warning("你还没有设置支付密码","立刻设置","index.php?app=account_security&act=authen_verify&mode=set_pay_password");
                     return ;
                    
                }
                $cur_name = '修改支付密码';
                $template = 'pay_password_edit.index.html';             
                break;
            case 'find_pay_password': //找回支付密码                
                if(!$accout_data['pay_password'])
                {
                    $this->show_warning("你还没有设置支付密码","立刻设置","index.php?app=account_security&act=authen_verify&mode=set_pay_password");
                     return ;
                    
                }
                $cur_name = '找回支付密码';
                $template = 'pay_password_find.index.html';
                $set_type = trim($_GET['set_type']); 
                if($set_type == 'mobile')//手机方式找回
                {
                    $step = isset($_SESSION['find_pay_password_step'])? $step : 1;
                    if($step == 2 && $_SESSION['find_pay_password_step']==2)
                    {
                        $step = 2;                       
                    }                   
                    if($step == 3 && isset($_SESSION['find_pay_password_step']))
                    {
                        unset($_SESSION['find_pay_password_step']);                        
                    }
                    if(!$accout_data['binding_mobile'])
                    {
                       $this->show_warning("请先绑定手机再设置",'goback', 'index.php?app=account_security&act=authen_verify&mode=set_mobile&from='.$this->_member_from);
                        return ; 
                    }
                    $cur_name = '绑定手机方式找回支付密码';
                    $template = 'pay_password_find_mobile.index.html'; 
                    $accout_data['hide_mobile']= hide_account($accout_data['binding_mobile'], 2);
                    
                }
                
                if($set_type == 'email')//邮箱方式找回
                {
                    $step = isset($_SESSION['find_pay_password_step'])? $step : 1;
                    if($step == 2 && $_SESSION['find_pay_password_step']==2)
                    {
                        $step = 2;                       
                    }                   
                    
                    if($step == 3 && isset($_SESSION['find_pay_password_step']))
                    {
                        unset($_SESSION['find_pay_password_step']);                        
                    }
                    
                    if(!$accout_data['binding_email'])
                    {
                       $this->show_warning("请先绑定邮箱再设置",'goback', 'index.php?app=account_security&act=authen_verify&mode=set_email&from='.$this->_member_from);
                        return ; 
                    }
                    $cur_name = '绑定邮箱方式设置支付密码';
                    $template = 'pay_password_find_email.index.html'; 
                    $accout_data['hide_email']= hide_account($accout_data['binding_email'], 1);
                    
                }
                break;              
            default:
                header("Location:index.php?app=account_security");
                break;


        }

        if ($this->_member_from == 'seller') {
            $this->_curlocal(LANG::get('seller_center'), 'index.php?app=seller', 
                             LANG::get('my_account'),'index.php?app=account_security&from=seller',
                             LANG::get('my_safety'),'index.php?app=account_security&from=seller',
                             $cur_name);
            $this->_config_seo('title', Lang::get('seller_center') . ' - ' . Lang::get('my_safety'));
        } else {
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', 
                             LANG::get('my_account'),'index.php?app=account_security&from=buyer', 
                             LANG::get('my_safety'),'index.php?app=account_security&from=buyer',
                             $cur_name);
            $this->_config_seo('title', Lang::get('buyer_center') . ' - ' . Lang::get('my_safety'));
        }
        $this->import_resource(array(
                'script' => 'jquery.plugins/jquery.validate.js,jquery.plugins/additional-methods.js,user.validate.js',
            ));

        /* 当前用户中心菜单 */
        $this->_curitem('my_safety');

        /* 当前所处子菜单 */
        $this->_curmenu('safety_list');
        $this->assign('step', $step);
        $this->assign('accout_data', $accout_data);
        $this->assign('safety_level', array('1' => '低','2' => '中','3' => '高'));
        $this->display($template);

    }
    
    /**
     *账号安全等级
     */
    function account_security_level()
    {
        $rs = array();
        $i = 0;
        $accout_security_set = '您的账户安全等级已经最高了';
        $accout_security = $this->_user_safety_mod->get(array('conditions'    => 'user_id = ' . $this->_user_id));
        if($accout_security['pay_password']){$i++;}
        if($accout_security['binding_email']){$i++;}
        if($accout_security['binding_mobile']){$i++;}
        if($i==0){$i=1;}
        if(!$accout_security['binding_email']){$accout_security_set = '提高账户安全等级：<a href="index.php?app=account_security&act=authen_verify&mode=set_email&from=' . $this->_member_from . '" class="set">绑定邮箱 »</a>';}
        if(!$accout_security['binding_mobile']){$accout_security_set = '提高账户安全等级：<a href="index.php?app=account_security&act=authen_verify&mode=set_mobile&from=' . $this->_member_from . '" class="set">绑定手机 »</a>';}
        if(!$accout_security['pay_password']){$accout_security_set = '提高账户安全等级：<a href="index.php?app=account_security&act=authen_verify&mode=set_pay_password&from=' . $this->_member_from . '" class="set">设置支付密码 »</a> &nbsp;&nbsp;使用万菱账户资金付款时使用、确认收货时使用，用于保障账户资金安全。';}
        $rs['level'] = $i;
        $rs['set'] = $accout_security_set;
       return $rs;
               
    }


}

?>